package cn.minsh.lims.eoms.modules.job.task;

import cn.minsh.lims.eoms.modules.app.entity.MaintenanceOrderEntity;
import cn.minsh.lims.eoms.modules.app.entity.PlanRemindEntity;
import cn.minsh.lims.eoms.modules.app.entity.PlanScopeEntity;
import cn.minsh.lims.eoms.modules.app.enums.BusiTypeEnum;
import cn.minsh.lims.eoms.modules.app.enums.OrderStatusEnum;
import cn.minsh.lims.eoms.modules.app.enums.RepairLeveEnum;
import cn.minsh.lims.eoms.modules.app.message.MaintenanceOrderRemindMsg;
import cn.minsh.lims.eoms.modules.app.message.RepairOrderMsg;
import cn.minsh.lims.eoms.modules.app.service.MaintenanceOrderService;
import cn.minsh.lims.eoms.modules.app.service.PlanRemindService;
import cn.minsh.lims.eoms.modules.app.service.PlanScopeService;
import cn.minsh.lims.eoms.modules.app.service.QueueSendMessageService;
import cn.minsh.lims.eoms.modules.app.utils.LocalDateUtils;
import cn.minsh.lims.eoms.modules.app.utils.QueueUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * 保养工单提醒
 */
@Component("checkMaintenanceOrderRemindTask")
public class CheckMaintenanceOrderRemindTask implements ITask{

    @Autowired
    private MaintenanceOrderService maintenanceOrderService ;
    @Autowired
    private PlanRemindService planRemindService ;
    @Autowired
    private QueueSendMessageService queueSendMessageService ;
    @Autowired
    private PlanScopeService planScopeService ;



    @Override
    public void run(String params) {
        //读取所有未开始的保养工单
        QueryWrapper<MaintenanceOrderEntity> wrapper = new QueryWrapper<>() ;
        wrapper.eq("status", OrderStatusEnum.NOT_STARTED.getCode()) ;
        wrapper.eq("is_del",0) ;
        List<MaintenanceOrderEntity> list = maintenanceOrderService.list(wrapper) ;
        if (!CollectionUtils.isEmpty(list)){
            List<Long> orderIdList = list.stream().map(MaintenanceOrderEntity::getId).collect(Collectors.toList()) ;
            QueryWrapper<PlanRemindEntity> remindEntityQueryWrapper = new QueryWrapper<>() ;
            remindEntityQueryWrapper.eq("type", BusiTypeEnum.MAINTENANCEORDER.getCode()) ;
            remindEntityQueryWrapper.eq("is_del",0) ;
            remindEntityQueryWrapper.in("plan_id",orderIdList) ;
            remindEntityQueryWrapper.orderByAsc("lead_time") ;
            List<PlanRemindEntity> planRemindList = planRemindService.list(remindEntityQueryWrapper) ;
            /*查询设备列表*/
            QueryWrapper<PlanScopeEntity> scopeEntityQueryWrapper = new QueryWrapper<>() ;
            scopeEntityQueryWrapper.eq("type", BusiTypeEnum.MAINTENANCEORDER.getCode()) ;
            scopeEntityQueryWrapper.eq("is_del", 0) ;
            scopeEntityQueryWrapper.in("plan_id",orderIdList) ;

            List<PlanScopeEntity> planScopeEntityList = planScopeService.list(scopeEntityQueryWrapper) ;

            if (!CollectionUtils.isEmpty(planRemindList)) {
                list.stream().forEach(e -> {
                    if (e.getAgentId()==null){
                        return;
                    }
                    planRemindList.stream().filter(p->p.getPlanId().equals(e.getId())).forEach(pp->{
                        Integer days = pp.getLeadTime() ;
                        Date remindDate = LocalDateUtils.addDateDays(e.getMaintenanceTime(),-days) ;
                        if (LocalDateUtils.compareDay(new Date(),remindDate)==0){
                            //TODO 同一天提醒经办人保养

                            MaintenanceOrderRemindMsg msg = new MaintenanceOrderRemindMsg() ;
                            msg.setTitle("保养工单提醒");
                            msg.setReceiver(String.valueOf(e.getAgentId()));
                            msg.setContent("你有一个保养工单需要处理");
                            msg.setCode(e.getCode());
                            List<Long> scopeIdList = planScopeEntityList.stream().filter(ppp->ppp.getPlanId().equals(e.getId())).map(PlanScopeEntity::getScopeId).collect(Collectors.toList());
                            msg.setScopeIdList(scopeIdList);
                            queueSendMessageService.sendMessage(QueueUtils.MAINTENANCE_ORDER_REMIND,msg);
                        }
                    }); ;
                });
            }
        }
    }
}
